//是否是Android 或 iOS
var u = navigator.userAgent, app = navigator.appVersion;
IS_ANDROID = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
IS_IOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
IS_WEIXIN = false;
IS_WEIBO = false;
IS_QQ = false;

var ua = navigator.userAgent.toLowerCase();//获取判断用的对象
if (ua.match(/MicroMessenger/i) == "micromessenger") {
    //在微信中打开
    IS_WEIXIN = true;
}
if (ua.match(/WeiBo/i) == "weibo") {
    //在新浪微博客户端打开
    IS_WEIBO = true;
}
if (ua.match(/QQ/i) == "qq") {
    //在QQ空间打开
    IS_QQ = true;
}

window.GetRequest = function () {
    if (typeof window.urlArg != "undefined") {
        return window.urlArg;
    }
    var url = location.search; //获取url中"?"符后的字串   
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    window.urlArg = theRequest;
    return theRequest;
};
window.urlArg = window.GetRequest();
window.h5Url = "https://spdblczx.cojoy.com.cn/";
window.h5UrlRequest = "https://spdblczx.cojoy.com.cn/";
window.details = [
    { title: "浦发包了一辆“列车”，您最想去哪儿看风景？", desc: "“绿创号”即将发车，请速上车" },
    { title: "注意！您有一张浦发“绿创号”车票待查收", desc: "上车，开启浦发银行绿色金融之旅" },
];
window.shareImgUrl = window.h5Url + "share.jpg";
var myWx = {
    ready: false,
    init: function () {
        $.ajax({
            type: 'GET',
            url: 'https://open.cojoy.com.cn/wechat/jssdk/getsign.php',
            dataType: 'json',
            data: {
                _url: window.location.href
            },
            success: function (data, textStatus, jqXHR) {
                wx.config({
                    beta: true,
                    debug: false,
                    appId: data.appId,
                    timestamp: data.timestamp,
                    nonceStr: data.nonceStr,
                    signature: data.signature,
                    jsApiList: [
                        'onMenuShareTimeline',
                        'onMenuShareAppMessage'
                    ]
                });

                wx.ready(this.onWxReady.bind(this));
            }.bind(this),
            error: this.onWxReady.bind(this)
        });
    },
    onWxReady: function () {
        $.ajax({
            type: 'GET',
            url: window.ADDRESSES.GET_USER_INFO,
            data: {},
            success: function (data, textStatus, jqXHR) {
                window.userInfo = data;
                cc.utils.msgHub.emit(cc.utils.macro.EVENTS.USER_INFO_READY);
            }.bind(this),
            error: function (e) {
                //错误
                console.log("window.ADDRESSES.GET_USER_INFO,error:", e);
                window.open(window.h5Url);
            }
        });

        try {
            this.ready = true;
            cc.utils.msgHub.emit(cc.utils.macro.EVENTS.WX_READY);
        } catch (e) { }

        try {
            cc.utils.soundMgr.playSound("sil");
            if (!cc.utils.soundMgr.bgmList.bgm) {
                cc.utils.soundMgr.playBgm("bgm");
            }

            console.log("myWx.playBgm,ok");
        } catch (e) {
            console.log("myWx.playBgm,fail");
        }

        // try {
        //     window.BGM = document.getElementById("bgm");
        //     BGM.play();
        // } catch (e) { }

        // try {
        //     cc.utils.soundMgr.playSound("sil");
        //     cc.utils.soundMgr.playBgm("bgm");
        // } catch (e) { }

        this.shareConfig();
        console.log("myWx::onWxReady");
    },
    shareConfig: function () {
        var td = window.details[Math.floor(Math.random() * window.details.length)];
        var shareInfoAppMessage = {
            title: td.title,
            desc: td.desc,
            link: window.h5Url,
            imgUrl: window.shareImgUrl,
            type: '',
            dataUrl: '',
            success: this.onShareFinish.bind(this),
            cancel: this.onShareFinish.bind(this)
        };
        wx.onMenuShareAppMessage(shareInfoAppMessage);

        var shareInfoTimeLine = {
            title: td.title + td.desc,
            link: window.h5Url,
            imgUrl: window.shareImgUrl,
            success: this.onShareFinish.bind(this),
            cancel: this.onShareFinish.bind(this)
        };
        wx.onMenuShareTimeline(shareInfoTimeLine);
    },
    onShareFinish: function () {
        this.shareConfig();
    }
};

window.myWx = myWx;